package com.leapp.sk.viewdelegate.login

import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R

/**
 * 用户协议视图托管层
 * @author  chaohaipeng  on 2018/7/10.
 */
class UserAgreementDelegate : BaseAppDelegate() {
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_user_agreement
    }


    /**
     * 设置标题栏
     */
    fun setTitle(title: String?) {
        val jtb_aua = getView<SKTopBar>(R.id.jtb_aua)
        if (!TextUtils.isEmpty(title)) {
            jtb_aua.setTitle(title)
        }
        jtb_aua.setEventInterface(object :SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<AppCompatActivity>().finish()
            }

            override fun rightOnClick() {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

        })

    }

        /**
         * WebView设置
         */
        fun setWebView(url: String) {
            if (!TextUtils.isEmpty(url)) {
                val wv_aua = getView<WebView>(R.id.wv_aua)
                val settings = wv_aua.getSettings()
                settings.cacheMode = WebSettings.LOAD_NO_CACHE  //从不加载缓存
                settings.allowFileAccess = true                 // 允许访问文件
                settings.databaseEnabled = true                  //支持数据库存储
                settings.setNeedInitialFocus(true)
                settings.loadsImagesAutomatically = true
                wv_aua.loadUrl(url)
                wv_aua.webViewClient = object : WebViewClient() {
                    override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
                        super.onPageStarted(view, url, favicon)
                    }

                    override fun onPageFinished(view: WebView, url: String) {
                        super.onPageFinished(view, url)
                    }
                }
                wv_aua.webChromeClient = object : WebChromeClient() {
                    override fun onProgressChanged(view: WebView, newProgress: Int) {
                        if (newProgress == 100) { // 网页加载完成
                        } else { // 加载中

                        }
                    }
                }
            }
        }


}